//
//  YJTrainingDetailTableViewController.swift
//  yuejian
//
//  Created by 闫子夜 on 2017/10/24.
//  Copyright © 2017年 gyy. All rights reserved.
//

import UIKit

class TrainingDetailTableViewController: UITableViewController {
    var videoPlayer:VideoPlayer!
    var hideState = false
    var train:YJTrainingModel!
    
    var commentList:NSArray = NSArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
        self.title = train.trainname as String
        NotificationCenter.default.addObserver(self, selector:#selector(TrainingDetailTableViewController.makeOrder),name:NSNotification.Name(rawValue: "Order"), object: nil)
        
        //添加评论
        let paraDict = ["id":train.train_id!] as NSDictionary
        YJNetworkMngTool.sharedNetWorkMngTool().YJNetwork_GetTrainCommentListParameters(paraDict , view: self.view) { (commentArray) in
//          self.commentList = commentArray
            self.tableView.reloadData()
        }
    }
    func viewConfig() {
        let label = UILabel(frame: CGRect(x: 15, y: 357, width: WIDTH-30, height: 40))
        label.text = "视频简介"
        label.textAlignment = .center
        label.layer.cornerRadius = 5
        label.layer.borderColor = UIColor.lightGray.cgColor
        label.layer.borderWidth = 1
        view.addSubview(label)
        
        videoPlayer = VideoPlayer(frame: CGRect(x: 0, y: 402, width: WIDTH, height: 200))
        videoPlayer.initialize(itemUrl:BB_VIDEOURL)
        view.addSubview(videoPlayer)
        videoPlayer.fullBtn.addTarget(self, action: #selector(fullScreen), for: .touchUpInside)
    }
    
    @objc func fullScreen(){
        let pw = videoPlayer.bounds.width
        let ph = videoPlayer.bounds.height
        if !hideState{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform =
                    CGAffineTransform(rotationAngle: CGFloat(Double.pi/180)*CGFloat(90)).concatenating(
                        CGAffineTransform(scaleX: WIDTH/ph,
                                          y: HEIGHT/pw)).concatenating(
                                            CGAffineTransform(translationX: 0,
                                                              y: -25.833-64))
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
        else{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform = .identity
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    
    
    
    
    func makeOrder(){
        self.performSegue(withIdentifier: "t2", sender: train)
        
    }
    
    override func prepare(for segue:UIStoryboardSegue, sender: Any?) {
        let orderController: YJOrderTableViewController = segue.destination as!  YJOrderTableViewController
        
        orderController.train = sender as! YJTrainingModel
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 3
        }else {
            return 1
        }
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var basecell: UITableViewCell = UITableViewCell()
        if indexPath.section == 0 {
            if (indexPath as NSIndexPath).row == 0 {
                let cell : YJTrainHeaderCell = tableView.dequeueReusableCell(withIdentifier: "YJTrainHeaderCell", for: indexPath) as! YJTrainHeaderCell
                cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CELLHEIGHT);
                
                tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine;
                
                let imageStr = "http://172.24.10.175/workout/Uploads/" + (train.thumb! as String)
                let imageURL = URL(string: imageStr as String)
                cell.trainImageView!.setImageWith(imageURL!, placeholderImage: UIImage(named: "placeHolder"))
                
                //训练名称
                cell.trainName.text = "\(train.trainname!)"
                //价格
                cell.price.text = "\(train.price!)元"
                //推荐指数
                cell.recommand.text = "\(train.recommand!)"
                //商店编号
                cell.shop_id.text = "\(train.train_id!)"
                basecell = cell
                
            } else if((indexPath as NSIndexPath).row == 1){
                let cell: YJTrainIntroduceCell = tableView.dequeueReusableCell(withIdentifier: "YJTrainIntroduceCell", for: indexPath) as! YJTrainIntroduceCell
                //先设置模型
                cell.train = train;
                //再算Cell的frame
                cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: YJTrainIntroduceCell.cellHeightWithPostinModel(train));
                basecell = cell;
            } else if((indexPath as NSIndexPath).row == 2) {
                
                let cell = YJTrainDetailCell.creatCell(tableView)
                cell.train = train
                basecell = cell
            }
        }

        return basecell
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if ((indexPath as NSIndexPath).row == 0) {
            return 120;
        }else if((indexPath as NSIndexPath).row == 1){
            //返回可变的cell高度 需要根据模型的内容进行计算
            return 170;
        }else{
            return 60;
        }
    }
    

    
    //设定section的footer
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    
    override var prefersStatusBarHidden: Bool{
        return hideState
    }
    
}

